﻿using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

// Token: 0x02000131 RID: 305
public class BackGroundChangeUI : MonoBehaviour
{
	// Token: 0x060006DD RID: 1757 RVA: 0x00028744 File Offset: 0x00026944
	private void Awake()
	{
		this.illCam = UnityEngine.Object.FindObjectOfType<IllusionCamera>();
		this.cam = this.illCam.GetComponent<Camera>();
		this.invoke = false;
		for (int i = 0; i < this.toggles.Length; i++)
		{
			this.toggles[i].onValueChanged.AddListener(new UnityAction<bool>(this.OnChangeToggle));
		}
		this.SetupMap();
		this.colorPicker.Setup("背景色", ConfigData.clearColor, false, new Action<Color>(this.OnChangeColor));
		this.SetupSkyBox();
		this.SetupPictures();
		this.toggles[(int)this.nowType].isOn = true;
		this.ChangeTab();
		this.invoke = true;
	}

	// Token: 0x060006DE RID: 1758 RVA: 0x00028800 File Offset: 0x00026A00
	private void SetupMap()
	{
		List<string> list = new List<string>();
		int mapNum = GlobalData.GetMapNum();
		for (int i = 0; i < mapNum; i++)
		{
			list.Add(GlobalData.GetMapShowName(i));
		}
		this.mapDropDown.options.Clear();
		this.mapDropDown.captionText.text = "マップ";
		this.mapDropDown.AddOptions(list);
		this.mapDropDown.onValueChanged.AddListener(new UnityAction<int>(this.OnChangeMap));
		for (int j = 0; j < this.mapTimes.Length; j++)
		{
			this.mapTimes[j].onValueChanged.AddListener(new UnityAction<bool>(this.OnChangeMapTime));
		}
	}

	// Token: 0x060006DF RID: 1759 RVA: 0x000288BC File Offset: 0x00026ABC
	private void SetupSkyBox()
	{
		this.skyboxList.Clear();
		this.skyboxDropDown.options.Clear();
		TextAsset text = AssetBundleLoader.LoadAsset<TextAsset>(GlobalData.assetBundlePath, "skybox", "skybox_list");
		CustomDataListLoader customDataListLoader = new CustomDataListLoader();
		customDataListLoader.Load(text);
		for (int i = 0; i < customDataListLoader.GetDataNum(); i++)
		{
			string data = customDataListLoader.GetData(0, i);
			string data2 = customDataListLoader.GetData(1, i);
			this.skyboxList.Add(data2);
			this.skyboxDropDown.options.Add(new Dropdown.OptionData(data));
		}
		this.skyboxDropDown.onValueChanged.AddListener(new UnityAction<int>(this.OnChangeSkybox));
	}

	// Token: 0x060006E0 RID: 1760 RVA: 0x00028970 File Offset: 0x00026B70
	private void SetupPictures()
	{
		this.pictureButton_next.onClick.AddListener(delegate()
		{
			this.PicturePageMove(1);
		});
		this.pictureButton_prev.onClick.AddListener(delegate()
		{
			this.PicturePageMove(-1);
		});
		this.pictureButton_next5.onClick.AddListener(delegate()
		{
			this.PicturePageMove(5);
		});
		this.pictureButton_prev5.onClick.AddListener(delegate()
		{
			this.PicturePageMove(-5);
		});
		for (int i = 0; i < this.pictureButtons.Length; i++)
		{
			int no = i;
			this.pictureButtons[i].onClick.AddListener(delegate()
			{
				this.OnClickPicture(no);
			});
		}
		this.pictures = Directory.GetFiles("UserData/Background", "*.png", SearchOption.AllDirectories);
		this.PicturePageMove(0);
	}

	// Token: 0x060006E1 RID: 1761 RVA: 0x00028A58 File Offset: 0x00026C58
	private void PicturePage()
	{
		int num = this.nowPicturePage;
		for (int i = 0; i < 9; i++)
		{
			int num2 = num * 9 + i;
			if (num2 < this.pictures.Length)
			{
				this.LoadImage_Thumbs(this.pictureButtons[i].image, this.pictures[num2]);
				this.pictureButtons[i].interactable = true;
			}
			else
			{
				this.pictureButtons[i].image.sprite = null;
				this.pictureButtons[i].interactable = false;
			}
		}
	}

	// Token: 0x060006E2 RID: 1762 RVA: 0x00028AE4 File Offset: 0x00026CE4
	private static Texture2D LoadPNG(string file)
	{
		byte[] array = null;
		FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
		if (fileStream == null)
		{
			return null;
		}
		using (BinaryReader binaryReader = new BinaryReader(fileStream))
		{
			try
			{
				array = binaryReader.ReadBytes((int)binaryReader.BaseStream.Length);
			}
			catch (Exception message)
			{
				GlobalData.Debug_LogWarning(message);
				array = null;
			}
			binaryReader.Close();
		}
		if (array == null)
		{
			return null;
		}
		Texture2D texture2D = new Texture2D(1, 1);
		texture2D.LoadImage(array);
		array = null;
		return texture2D;
	}

	// Token: 0x060006E3 RID: 1763 RVA: 0x00028B84 File Offset: 0x00026D84
	private void LoadImage_Thumbs(Image image, string file)
	{
		Texture2D texture2D = BackGroundChangeUI.LoadPNG(file);
		TextureScale.Bilinear(texture2D, 64, 40);
		Vector2 vector = new Vector2(64f, 40f);
		Rect rect = new Rect(Vector2.zero, vector);
		Vector2 pivot = vector * 0.5f;
		if (texture2D != null)
		{
			image.sprite = Sprite.Create(texture2D, rect, pivot, 100f, 0U, SpriteMeshType.FullRect);
		}
		else
		{
			image.sprite = null;
		}
	}

	// Token: 0x060006E4 RID: 1764 RVA: 0x00028BFC File Offset: 0x00026DFC
	private void LoadImage_Raw(Image image, string file)
	{
		Texture2D texture2D = BackGroundChangeUI.LoadPNG(file);
		Vector2 vector = new Vector2((float)texture2D.width, (float)texture2D.height);
		Rect rect = new Rect(Vector2.zero, vector);
		Vector2 pivot = vector * 0.5f;
		if (texture2D != null)
		{
			image.sprite = Sprite.Create(texture2D, rect, pivot, 100f, 0U, SpriteMeshType.FullRect);
		}
		else
		{
			image.sprite = null;
		}
	}

	// Token: 0x060006E5 RID: 1765 RVA: 0x00028C6C File Offset: 0x00026E6C
	private void PicturePageMove(int move)
	{
		int num = this.pictures.Length / 9;
		if (num > 0 && this.pictures.Length % 9 == 0)
		{
			num--;
		}
		num++;
		this.nowPicturePage = (num + this.nowPicturePage + move) % num;
		this.picturePageText.text = this.nowPicturePage + 1 + " / " + num;
		this.PicturePage();
	}

	// Token: 0x060006E6 RID: 1766 RVA: 0x00028CE4 File Offset: 0x00026EE4
	private void OnClickPicture(int no)
	{
		int num = this.nowPicturePage * 9 + no;
		bool enabled;
		if (no < this.pictures.Length)
		{
			this.LoadImage_Raw(this.backImage, this.pictures[num]);
			enabled = true;
		}
		else
		{
			enabled = false;
		}
		this.backImage.enabled = enabled;
	}

	// Token: 0x060006E7 RID: 1767 RVA: 0x00028D38 File Offset: 0x00026F38
	private void Update()
	{
		if (this.changeToggle)
		{
			this.ChangeTab();
			this.changeToggle = false;
		}
		if (this.changeMapTime)
		{
			this.ChangeMap();
			this.changeMapTime = false;
		}
		if (this.nowType == BackGroundChangeUI.TYPE.PICTURE)
		{
			this.UpdatePicture();
		}
		else
		{
			this.backImage.enabled = false;
		}
	}

	// Token: 0x060006E8 RID: 1768 RVA: 0x00028D98 File Offset: 0x00026F98
	private void UpdatePicture()
	{
		if (!this.backImage.enabled || this.backImage.sprite.texture == null)
		{
			return;
		}
		bool isOn = this.pictureToggle_fitW.isOn;
		bool isOn2 = this.pictureToggle_fitH.isOn;
		int width = this.backImage.sprite.texture.width;
		int height = this.backImage.sprite.texture.height;
		Vector2 sizeDelta = new Vector2((float)width, (float)height);
		if (isOn && isOn2)
		{
			sizeDelta.x = (float)Screen.width;
			sizeDelta.y = (float)Screen.height;
		}
		else if (isOn)
		{
			float num = sizeDelta.y / sizeDelta.x;
			sizeDelta.x = (float)Screen.width;
			sizeDelta.y = sizeDelta.x * num;
		}
		else if (isOn2)
		{
			float num2 = sizeDelta.x / sizeDelta.y;
			sizeDelta.y = (float)Screen.height;
			sizeDelta.x = sizeDelta.y * num2;
		}
		this.backImage.rectTransform.sizeDelta = sizeDelta;
	}

	// Token: 0x060006E9 RID: 1769 RVA: 0x00028ED0 File Offset: 0x000270D0
	private void ChangeTab()
	{
		for (int i = 0; i < this.toggles.Length; i++)
		{
			this.mains[i].SetActive(this.toggles[i].isOn);
			if (this.toggles[i].isOn)
			{
				this.nowType = (BackGroundChangeUI.TYPE)i;
			}
		}
		if (this.nowType == BackGroundChangeUI.TYPE.OBJ)
		{
			this.cam.clearFlags = CameraClearFlags.Skybox;
			this.ChangeMap();
		}
		else if (this.nowType == BackGroundChangeUI.TYPE.SKYBOX)
		{
			this.cam.clearFlags = CameraClearFlags.Skybox;
			this.cam.backgroundColor = ConfigData.clearColor;
			this.OnChangeSkybox(this.skyboxDropDown.value);
		}
		else if (this.nowType == BackGroundChangeUI.TYPE.PICTURE)
		{
			this.cam.clearFlags = CameraClearFlags.Color;
			this.cam.backgroundColor = ConfigData.clearColor;
			this.backImage.enabled = (this.backImage.sprite != null);
		}
		else if (this.nowType == BackGroundChangeUI.TYPE.COLOR)
		{
			this.cam.clearFlags = CameraClearFlags.Color;
			this.cam.backgroundColor = ConfigData.clearColor;
		}
		if (this.nowType != BackGroundChangeUI.TYPE.PICTURE)
		{
			this.backImage.enabled = false;
		}
		if (this.map != null)
		{
			this.map.gameObject.SetActive(this.nowType == BackGroundChangeUI.TYPE.OBJ);
		}
		bool flag = this.map != null && this.map.gameObject.activeInHierarchy;
		this.defRefProbe.gameObject.SetActive(!flag);
		if (this.nowType != BackGroundChangeUI.TYPE.OBJ)
		{
			this.noMapLight.gameObject.SetActive(true);
			this.noMapLight.color = Color.HSVToRGB(0f, 0f, 0.8235294f);
			this.noMapLight.intensity = 1f;
			this.lightCtrl.SetupLight(this.noMapLight.transform, false);
		}
		else
		{
			this.noMapLight.gameObject.SetActive(false);
		}
	}

	// Token: 0x060006EA RID: 1770 RVA: 0x000290F0 File Offset: 0x000272F0
	private void ChangeMap()
	{
		int value = this.mapDropDown.value;
		int num = 0;
		for (int i = 0; i < this.mapTimes.Length; i++)
		{
			if (this.mapTimes[i].isOn)
			{
				num = i;
				break;
			}
		}
		string[] array = new string[]
		{
			"day",
			"evening",
			"night_light",
			"night_dark"
		};
		string text = GlobalData.GetMapAssetName(value) + "_" + array[num];
		if (this.map != null)
		{
			UnityEngine.Object.Destroy(this.map.gameObject);
		}
		string assetBundleName = "map/" + text;
		AssetBundleController assetBundleController = new AssetBundleController();
		assetBundleController.OpenFromFile(GlobalData.assetBundlePath, assetBundleName);
		GameObject gameObject = assetBundleController.LoadAndInstantiate<GameObject>(text);
		this.map = gameObject.GetComponent<Map>();
		this.map.name = text;
		LightMapControl componentInChildren = this.map.GetComponentInChildren<LightMapControl>();
		componentInChildren.Apply();
		assetBundleController.Close(false);
		this.lightCtrl.SetupLight(this.map.lightRoot, false);
		Resources.UnloadUnusedAssets();
	}

	// Token: 0x060006EB RID: 1771 RVA: 0x0002921C File Offset: 0x0002741C
	private void OnChangeToggle(bool flag)
	{
		if (!this.invoke)
		{
			return;
		}
		if (flag)
		{
			this.changeToggle = true;
			SystemSE.Play(SystemSE.SE.CHOICE);
		}
	}

	// Token: 0x060006EC RID: 1772 RVA: 0x00029240 File Offset: 0x00027440
	private void OnChangeSkybox(int no)
	{
		if (no >= 0 && no < this.skyboxList.Count)
		{
			string assetName = this.skyboxList[no];
			Material material = AssetBundleLoader.LoadAsset<Material>(GlobalData.assetBundlePath, "skybox", assetName);
			Skybox component = this.illCam.GetComponent<Skybox>();
			component.material = material;
			Resources.UnloadUnusedAssets();
		}
	}

	// Token: 0x060006ED RID: 1773 RVA: 0x0002929C File Offset: 0x0002749C
	private void OnChangeMap(int no)
	{
		this.ChangeMap();
	}

	// Token: 0x060006EE RID: 1774 RVA: 0x000292A4 File Offset: 0x000274A4
	private void OnChangeMapTime(bool flag)
	{
		if (!this.invoke)
		{
			return;
		}
		if (flag)
		{
			this.changeMapTime = true;
			SystemSE.Play(SystemSE.SE.CHOICE);
		}
	}

	// Token: 0x060006EF RID: 1775 RVA: 0x000292C8 File Offset: 0x000274C8
	private void OnChangeColor(Color color)
	{
		ConfigData.clearColor = color;
		this.cam.backgroundColor = color;
	}

	// Token: 0x040006C5 RID: 1733
	[SerializeField]
	private Light noMapLight;

	// Token: 0x040006C6 RID: 1734
	[SerializeField]
	private LightController lightCtrl;

	// Token: 0x040006C7 RID: 1735
	[SerializeField]
	private Toggle[] toggles;

	// Token: 0x040006C8 RID: 1736
	[SerializeField]
	private GameObject[] mains;

	// Token: 0x040006C9 RID: 1737
	[SerializeField]
	private Dropdown mapDropDown;

	// Token: 0x040006CA RID: 1738
	[SerializeField]
	private Toggle[] mapTimes;

	// Token: 0x040006CB RID: 1739
	[SerializeField]
	private Dropdown skyboxDropDown;

	// Token: 0x040006CC RID: 1740
	[SerializeField]
	private ColorChangeButton colorPicker;

	// Token: 0x040006CD RID: 1741
	[SerializeField]
	private Button[] pictureButtons;

	// Token: 0x040006CE RID: 1742
	[SerializeField]
	private Text picturePageText;

	// Token: 0x040006CF RID: 1743
	[SerializeField]
	private Button pictureButton_next;

	// Token: 0x040006D0 RID: 1744
	[SerializeField]
	private Button pictureButton_prev;

	// Token: 0x040006D1 RID: 1745
	[SerializeField]
	private Button pictureButton_next5;

	// Token: 0x040006D2 RID: 1746
	[SerializeField]
	private Button pictureButton_prev5;

	// Token: 0x040006D3 RID: 1747
	[SerializeField]
	private Toggle pictureToggle_fitW;

	// Token: 0x040006D4 RID: 1748
	[SerializeField]
	private Toggle pictureToggle_fitH;

	// Token: 0x040006D5 RID: 1749
	[SerializeField]
	private Image backImage;

	// Token: 0x040006D6 RID: 1750
	[SerializeField]
	private ReflectionProbe defRefProbe;

	// Token: 0x040006D7 RID: 1751
	private bool changeToggle;

	// Token: 0x040006D8 RID: 1752
	private bool changeMapTime;

	// Token: 0x040006D9 RID: 1753
	private bool invoke = true;

	// Token: 0x040006DA RID: 1754
	private BackGroundChangeUI.TYPE nowType = BackGroundChangeUI.TYPE.SKYBOX;

	// Token: 0x040006DB RID: 1755
	private IllusionCamera illCam;

	// Token: 0x040006DC RID: 1756
	private Camera cam;

	// Token: 0x040006DD RID: 1757
	private Map map;

	// Token: 0x040006DE RID: 1758
	private List<string> skyboxList = new List<string>();

	// Token: 0x040006DF RID: 1759
	private string[] pictures;

	// Token: 0x040006E0 RID: 1760
	private int nowPicturePage;

	// Token: 0x02000132 RID: 306
	public enum TYPE
	{
		// Token: 0x040006E2 RID: 1762
		OBJ,
		// Token: 0x040006E3 RID: 1763
		SKYBOX,
		// Token: 0x040006E4 RID: 1764
		PICTURE,
		// Token: 0x040006E5 RID: 1765
		COLOR,
		// Token: 0x040006E6 RID: 1766
		NUM
	}
}
